/**
 * fileName:  EfficacyImpl.java
 * author:  Lei
 * Company:  Cloudland Studio
 * Createdate:  2014-6-3 下午10:40:21
 *
 * All rights Reserved, Designed By cloudland Copyright(C) 2010-2011
 *   
 * Modification  History:
 *     Date           Author        Version        
 * -------------------------------------------
 * 2014-6-3          Lei            1.0        
 *
 * Why & What is modified:
 * 1.0: 文件创建
 */
package org.cloudland.commercial.masterdata.handler.support;

import java.util.ArrayList;
import java.util.List;

import org.cloudland.commercial.masterdata.bean.EfficacyInfo;
import org.cloudland.commercial.masterdata.handler.EfficacyStore;
import org.cloudland.commercial.masterdata.store.PageResult;
import org.cloudland.commercial.masterdata.store.entity.EfficacyEntity;
import org.cloudland.commercial.masterdata.store.entity.PageQueryEntity;
import org.cloudland.commercial.masterdata.store.read.EfficacyReader;
import org.cloudland.commercial.masterdata.store.write.EfficacyWriter;

 /**
 * <pre> 
 * TODO 请充分描述该类提供的作用, 或与其他类会有关联的使用。
 * 使用 java.lang.Object#toString(), 或者 {@link java.lang.Character Character} 等标签
 * </pre>
 * @ClassName  EfficacyImpl
 * @author  Lei
 * @version  %I%, %G%
 * @see  
 * @since   JDK1.6
 *
 */
public class EfficacyImpl implements EfficacyStore {
	
	/**
	 * efficacyReader 功效字典查询操作
	 */
	private EfficacyReader efficacyReader;
	
	/**
	 * efficacyWriter 功效字典更新操作
	 */
	private EfficacyWriter efficacyWriter;

	/**
	 * <pre>
	 * TODO 请充分描述该构造函数的作用，及其参数的作用。
	 * </pre>
	 *   
	 */
	public EfficacyImpl(EfficacyWriter efficacyWriter, EfficacyReader efficacyReader) {
		this.efficacyWriter = efficacyWriter;
		this.efficacyReader = efficacyReader;
	}

	/**
	 * @return
	 */
	public EfficacyInfo createEfficacyInfo() {
		return new EfficacyBean(efficacyWriter, efficacyReader);
	}

	/**
	 * @param efficacyId
	 * @return
	 */
	public EfficacyInfo findEfficacyInfoById(String efficacyId) {
		EfficacyEntity efficacyEntity = efficacyReader.findEfficacyById(efficacyId);
		return new EfficacyBean(efficacyEntity, efficacyWriter, efficacyReader);
	}

	/**
	 * @return
	 */
	public EfficacyInfo[] findEfficacyInfoAll() {
		/**PageQueryEntity<EfficacyEntity> result = efficacyReader.findEfficacyForPage(0, 0);
		
		EfficacyInfo[] array = new EfficacyInfo[result.count()];
		int i = 0;
		for (EfficacyEntity en : result.list()) {
			array[i] = new EfficacyBean(en, efficacyWriter);
			i++;
		}*/
		List<EfficacyEntity> efficacyList = efficacyReader.findAllEfficacy();
		if(null == efficacyList)
		{
			return null;
		}
		EfficacyInfo[] list = new EfficacyInfo[efficacyList.size()];
		for(int i = 0; i < efficacyList.size(); i++)
		{
			list[i] = new EfficacyBean(efficacyList.get(i), efficacyWriter, efficacyReader);
		}
		return list;
	}
	
	/**
	 * @param name
	 * @param startNo
	 * @param pageSize
	 * @return
	 */
	public PageResult<EfficacyInfo> findEfficacyInfoByName(String name, Integer startNo, Integer pageSize) {
		
		PageQueryEntity<EfficacyEntity> result = efficacyReader.findEfficacyByName(name, startNo, pageSize);
		PageQueryEntity<EfficacyInfo> pageResult = new PageQueryEntity<EfficacyInfo>(result.count(), new ArrayList<EfficacyInfo>());
		
		for (EfficacyEntity en : result.list()) {
			pageResult.list().add(new EfficacyBean(en, efficacyWriter, efficacyReader));
		}
		
		return pageResult;
	}
	
	/**
	 * @param startNo
	 * @param pageSize
	 * @return
	 */
	public PageResult<EfficacyInfo> findPageEfficacyInfo(Integer startNo, Integer pageSize) {
		
		PageQueryEntity<EfficacyEntity> result = efficacyReader.findEfficacyForPage(startNo, pageSize);
		PageQueryEntity<EfficacyInfo> pageResult = new PageQueryEntity<EfficacyInfo>(result.count(), new ArrayList<EfficacyInfo>());
		
		for (EfficacyEntity en : result.list()) {
			pageResult.list().add(new EfficacyBean(en, efficacyWriter, efficacyReader));
		}
		
		return pageResult;
	}

	/**
	 * @param efficacyId
	 * @return
	 */
	public Boolean removeEfficacyInfo(String efficacyId) {
		
		EfficacyEntity entityDao = new EfficacyEntity(efficacyId, "", "", 1);
		efficacyWriter.delete(entityDao);
		
		return true;
	}

}
